﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZFSInterface.User;

namespace ZFSData.Manager
{

    /// <summary>
    /// 用户管理器 
    /// </summary>
    class UserManager : IUser
    {
        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="account"></param>
        public void Logout(string account)
        {
            using (var db = new ZFSConfig())
            {
                var model = db.tb_User.FirstOrDefault(t => t.Account.Equals(account));
                model.FlagOnline = "0";
                DbEntityEntry dbEntity = db.Entry<tb_User>(model);
                dbEntity.State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Update(tb_User user)
        {
            using (var db = new ZFSConfig())
            {
                DbEntityEntry dbEntity = db.Entry<tb_User>(user);
                dbEntity.State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns>结果</returns>
        public bool Add(tb_User user)
        {
            using (var db = new ZFSConfig())
            {
                db.Set<tb_User>().Add(user);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="account">账号</param>
        /// <returns>结果</returns>
        public bool Delete(string account)
        {
            using (var db = new ZFSConfig())
            {
                var model = db.tb_User.FirstOrDefault(q => q.Account.Equals(account));
                db.Set<tb_User>().Attach(model);
                db.Set<tb_User>().Remove(model);
                return db.SaveChanges() > 0 ? true : false;
            }
        }

        /// <summary>
        /// 用户是否存在
        /// </summary>
        /// <param name="account">账号</param>
        /// <returns>结果</returns>
        public bool Exits(string account)
        {
            using (var db = new ZFSConfig())
            {
                var model = db.tb_User.FirstOrDefault(q => q.Account.Equals(account));
                if (model != null)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="account">账号</param>
        /// <returns>结果</returns>
        public tb_User GetUser(string account)
        {
            using (var db = new ZFSConfig())
            {
                return db.tb_User.FirstOrDefault(q => q.Account.Equals(account));
            }
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns>集合</returns>
        public List<tb_User> GetUsers(string search)
        {
            using (var db = new ZFSConfig())
            {
                return db.tb_User.Where(q => q.UserName.Contains(search) || q.Account.Contains(search)).ToList();
            }
        }

        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="Account">账号</param>
        /// <param name="Password">密码</param>
        /// <returns>信息</returns>
        public tb_User Login(string Account, string Password)
        {
            using (var db = new ZFSConfig())
            {
                var userList = db.View_UserAuthority.Where(t => t.Account.Equals("admin")).ToList();

                return db.tb_User.FirstOrDefault(q => q.Account.Equals(Account) && q.Password.Equals(Password));
            }
        }

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public List<View_UserAuthority> GetAuthority(string account)
        {
            using (var db = new ZFSConfig())
            {
                return db.View_UserAuthority.Where(t => t.Account.Equals(account)).ToList();
            }
        }
    }
}
